package org.example.completed.controller;

import org.example.completed.utils.AliOSSUtils;
import org.example.completed.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;


@RestController
public class ImageUploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 保存图片到阿里云并回显图片
     * @param file 上传的文件
     * @return 回显页面的路径
     */
    @PostMapping("/user/upload")
    public ApiResponse<String> uploadImage(@RequestParam("file") MultipartFile file) {
        // 检查是否上传了文件
        if (file.isEmpty()) {
            return ApiResponse.failure("上传文件为空");
        }

        try {
            String url = aliOSSUtils.upload(file);
            return ApiResponse.success(url,"上传成功");
        } catch (IOException e) {
            // 捕获文件上传的异常
            e.printStackTrace();
            return null;
        }
    }
}

